<?php

namespace think\wework\Api;

use GuzzleHttp\Exception\GuzzleException;
use think\wework\Abstracts\WeworkApiWithToken;

class ExternalContact extends WeworkApiWithToken
{
    private static ?ExternalContact $instance = null;

    public static function getInstance(): ExternalContact
    {
        if (!self::$instance instanceof ExternalContact) {
            self::$instance = new self();
        }
        return self::$instance;
    }

    /**
     * 获取客户列表
     * @param string $userId
     * @return array
     */
    public function list(string $userId): array
    {
        $query = ['userid' => $userId];
        return $this->httpClient->get('externalcontact/list', compact('query'))->toArray();
    }

    /**
     * 获取外部联系人详情
     * @param string $externalUserId
     * @return array
     */
    public function get(string $externalUserId): array
    {
        $query = ['external_userid' => $externalUserId];
        return $this->httpClient->get('externalcontact/get', compact('query'))->toArray();
    }

    /**
     * 批量获取客户详情
     * @param array $userIds
     * @param string $cursor
     * @return array
     */
    public function batchGet(array $userIds, string $cursor = ''): array
    {
        $json = ['userid_list' => $userIds, 'cursor' => $cursor, 'limit' => 100];
        return $this->httpClient->post('externalcontact/batch/get_by_user', compact('json'))->toArray();
    }

    /**
     * 修改客户备注信息
     * @param array $userId
     * @param string $externalUserId
     * @param string $remark
     * @param string $description
     * @param string $remarkCompany
     * @param array $remarkMobiles
     * @param string $remarkPicMediaId
     * @return array
     */
    public function remark(array $userId, string $externalUserId, string $remark = '', string $description = '', string $remarkCompany = '', array $remarkMobiles = [], string $remarkPicMediaId = ''): array
    {
        $json = [
            'userid' => $userId,
            'external_userid' => $externalUserId
        ];
        if (!empty($remark)) $json['remark'] = $remark;
        if (!empty($description)) $json['description'] = $description;
        if (!empty($remarkCompany)) $json['remark_company'] = $remarkCompany;
        if (!empty($remarkMobiles)) $json['remark_mobiles'] = $remarkMobiles;
        if (!empty($remarkPicMediaId)) $json['remark_pic_mediaid'] = $remarkPicMediaId;

        return $this->httpClient->post('externalcontact/remark', compact('json'))->toArray();
    }

    /**
     * 发送新客户欢迎语
     * @param string $welcomeCode
     * @param string $welcomeMessage
     * @param array $attachments
     * @return array
     */
    public function welcome(string $welcomeCode, string $welcomeMessage, array $attachments = []): array
    {
        $json = [
            'welcome_code' => $welcomeCode,
            'text' => [
                'content' => $welcomeMessage
            ]
        ];
        if (!empty($attachments)) $json['attachments'] = $attachments;

        return $this->httpClient->post('externalcontact/send_welcome_msg', compact('json'))->toArray();

    }

    /**
     * 客户联系「联系我」
     * @param int $type 类型 1-单人，2-多人
     * @param array $user 用户ID数组
     * @param bool $skipVerify 外部客户添加时是否无需验证
     * @param string $state 企业自定义的state参数，用于区分不同的添加渠道
     * @param string $remark 联系方式的备注信息，用于助记
     * @return array
     */
    public function ContactWayAdd(int $type = 1, array $user = [], bool $skipVerify = true, string $state = '', string $remark = ''): array
    {
        $json = [
            'type' => $type,
            'scene' => 2,
            'remark' => $remark,
            'skip_verify' => $skipVerify,
            'state' => $state,
            'user' => $type == 1 ? array_splice($user, 0, 1) : $user
        ];

        return $this->httpClient->post('externalcontact/add_contact_way', compact('json'))->toArray();
    }

    /**
     * 获取企业已配置的「联系我」列表
     * @param string $cursor
     * @param int $start_time
     * @param int $end_time
     * @param int $limit
     * @return array
     */
    public function ContactWayList(string $cursor = '', int $start_time = 0, int $end_time = 0, int $limit = 1000): array
    {
        $json = ['limit' => $limit];

        if(!empty($cursor)) $json['cursor'] = $cursor;
        if($start_time > 0) $json['start_time'] = $start_time;
        if($end_time > 0) $json['end_time'] = $end_time;

        return $this->httpClient->post('externalcontact/list_contact_way', compact('json'))->toArray();
    }

    /**
     * 获取企业已配置的「联系我」方式
     * @param string $config_id
     * @return array
     */
    public function ContactWayInfo(string $config_id): array
    {
        $json = ['config_id' => $config_id];

        return $this->httpClient->post('externalcontact/get_contact_way', compact('json'))->toArray();
    }

    /**
     * 更新企业已配置的「联系我」方式
     * @param string $config_id
     * @param array $user
     * @param bool $skipVerify
     * @param string $state
     * @param string $remark
     * @return array
     */
    public function ContactWayEdit(string $config_id, array $user = [], bool $skipVerify = true, string $state = '', string $remark = ''): array
    {
        $json = [
            'config_id' => $config_id,
            'remark' => $remark,
            'skip_verify' => $skipVerify,
            'state' => $state,
            'user' => $user
        ];

        return $this->httpClient->post('externalcontact/update_contact_way', compact('json'))->toArray();

    }

    /**
     * 删除企业已配置的「联系我」方式
     * @param string $config_id
     * @return array
     */
    public function ContactWayDel(string $config_id): array
    {
        $json = ['config_id' => $config_id];

        return $this->httpClient->post('externalcontact/del_contact_way', compact('json'))->toArray();
    }
}
